package hellotabwidget.pack;

import java.sql.Date;

import hellotabwidget.pack.R;

import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;

public class RegisterActivity extends Activity implements android.view.View.OnClickListener {
	
	Button save;
	Button clear;
	EditText date;
	EditText expense;
	EditText category;
	EditText amount;
	int day;
	int month;
	int year;
	DatePicker datePicker;
	Spinner spinner;
		
		@Override
	    protected void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.register_layout);
	        save = (Button) findViewById(R.id.saveButtonId);
	        clear = (Button) findViewById(R.id.clearButtonId);
	        expense = (EditText) findViewById(R.id.expenseFieldId);
	        amount = (EditText) findViewById(R.id.sumFieldId);
	        datePicker=(DatePicker)findViewById(R.id.datePicker1);
	        spinner = (Spinner) findViewById(R.id.spinner);
	        
	        save.setOnClickListener(this);
	        clear.setOnClickListener(this);
	        
	        ArrayAdapter adapter = ArrayAdapter.createFromResource(
	        		this, R.array.category, android.R.layout.simple_spinner_item);
	         		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	         		spinner.setAdapter(adapter);
	         		}


		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			switch(arg0.getId()) {
			case R.id.saveButtonId:
				
				boolean didItWork = true;
				try {
					
					//String date = "year";
					Date date = new Date(datePicker.getYear() - 1900, datePicker.getMonth(), datePicker.getDayOfMonth());
					String dayDate = date.toString();
					String getSpinner = spinner.getSelectedItem().toString(); 
					String expenseFieldContent = expense.getText().toString();
					String sumFieldContent = amount.getText().toString();
					int amount = Integer.parseInt(sumFieldContent);
					int id = 1;
					EconomyDb db = new EconomyDb(RegisterActivity.this);
					db.open();
					db.createEntry(id, dayDate, expenseFieldContent, getSpinner, amount);
					//id++;
					db.close();
				
				}
				catch(Exception e) {
					didItWork = false;
					String error = e.toString();
					Dialog d = new Dialog(this);
					d.setTitle("Not working!");
					TextView tv = new TextView(this);
					tv.setText(error);
					d.setContentView(tv);
					d.show();
				}
				finally {
					if(didItWork) {
						Dialog d = new Dialog(this);
						d.setTitle("It works");
						TextView tv = new TextView(this);
						tv.setText("Success");
						d.setContentView(tv);
						d.show();
						
					}
					
				
				}
				break;

				
				case R.id.clearButtonId:
					expense.setText("");
					amount.setText(null);
					
					break;
					
				
			}
		}
	}
